package com.trydofor.a9task.base.code.help;

import com.trydofor.a9task.base.code.sysd.Bool;
import com.trydofor.aloe.exception.Exceptions;


/** 
 * @author : Shi Rongjiu (www.trydofor.com)
 * @date   : Dec 17, 2012
 * @see    : 
 */

public abstract class CodeHelp {
    
    /**
     * change JAVA_NME(underscore naming) to JavaName(java naming)
     * @param name the underscore naming
     * @return
     */
    public static String nameUnder2Java(String name){
        StringBuilder sb = new StringBuilder(name.length());
        boolean head = true;
        for (int i = 0; i < name.length(); i++) {
            char c = name.charAt(i);
            if(c == '_'){
                head = true;
                continue;
            }
            if(head){
                sb.append(azUpper(c));
                head = false;
            }else{
                sb.append(azLower(c));
            }
        }
        return sb.toString();
    }
    
    /**
     * change JavaName(java naming) to JAVA_NME(underscore naming)
     * @param name the java naming
     * @return
     */
    public static String nameJava2Under(String name){
        StringBuilder sb = new StringBuilder(name.length());
        sb.append(azUpper(name.charAt(0)));
        for (int i = 1; i < name.length(); i++) {
            char c = name.charAt(i);
            if (c >= 'A' && c <= 'Z') {
                sb.append('_');
            }
            sb.append(azUpper(c));
        }
        return sb.toString();
    }
    
    /**
     * AZ to az
     * @param c the AZ
     * @return
     */
    public static char azLower(char c) {
        if (c >= 'A' && c <= 'Z') {
            return (char) (c + 32);
        }
        return c;
    }
    
    /**
     * az to Az
     * @param c the az
     * @return
     */
    public static char azUpper(char c){
        if (c >= 'a' && c <= 'z') {
            return (char) (c - 32);
        }
        return c;
    }
    
    /**
     * get the pure sequence,range 2^6=[1,64]
     * 
     * @param seq the model seq
     * @return
     */
    public static int modelSeqPure(int seq) {
        return seq & 0x3F;
    }
    
    /**
     * @see #modelSeqPure(int)
     * @param cd the ModelSeq code
     * @return
     */
    public static int modelSeqPure(Code cd) {
        Exceptions.checkNullPointer(cd);
        return modelSeqPure(cd.main().getId());
    }
    
    /**
     * get Bool code
     * 
     * @param bool
     * @return
     */
    public static int bool(boolean bool) {
        return bool ? Bool.$TRUE : Bool.$FALSE;
    }
}


